home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / overview / moreisbetter / mib-clients / demos / moreopenandsavedemo / answershell.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  9.2 KB  |  387 lines

  1. /*
  2.     File:        AnswerShell.c
  3.  
  4.     Contains:    shell for MoreOpenAndSaveDemo
  5.  
  6.     Written by:    Pete Gontier
  7.  
  8.     Copyright:    Copyright (c) 1997-1998 Apple Computer, Inc.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.  
  20.         <10>     2/12/99    PCG     more sensible DebugStr message for case in which StandardGetFile
  21.                                     fitler is passed a directory
  22.          <9>      2/9/99    PCG     QDGlobals
  23.          <8>     1/22/99    PCG     TARGET_CARBON and more test cases
  24.          <7>     1/18/99    PCG     better QuickTime support
  25.          <6>     1/18/99    PCG     more Put customization support
  26.          <5>      1/7/99    PCG     starting: call Standard FIle when Nav is not avail and dialog
  27.                                     hook support
  28.          <4>     7/27/98    PCG     remove warnings re: missing params because Quinn treats warnings
  29.                                     as errors
  30.          <3>     7/24/98    PCG     remove #include "MoveableModalDialog.h"
  31.          <2>     7/11/98    PCG     add header
  32. */
  33.  
  34.  
  35. #define OLDROUTINELOCATIONS        0
  36. #define OLDROUTINENAMES            0
  37. #define SystemSevenOrLater        1
  38.  
  39. #include "MoreOpenAndSave.h"
  40. #include "MoreDialogs.h"
  41. #include "MoreToolbox.h"
  42.  
  43. #include <TextUtils.h>
  44. #include <Sound.h>
  45.  
  46. enum 
  47. {
  48.     kResID_Base = 128,
  49.     kResID_DialogItemList_CustomGetFile = kResID_Base,
  50.     kResID_DialogItemList_CustomPutFile = kResID_DialogItemList_CustomGetFile,
  51.     kResID_DialogItemList_MainWindow
  52. };
  53.  
  54. static pascal Boolean CustomFileFilterProc (CInfoPBPtr cipbp, void *)
  55. {
  56.     Boolean dropIt = false;
  57.  
  58.     if (!(cipbp->hFileInfo.ioFlAttrib & ioDirMask))
  59.         if (cipbp->hFileInfo.ioFlFndrInfo.fdCreator != 'CWIE')
  60.             dropIt = true;
  61.  
  62.     return dropIt;
  63. }
  64.  
  65. static pascal Boolean StandardFileFilterProc (CInfoPBPtr cipbp)
  66. {
  67.     Boolean dropIt = false;
  68.  
  69.     //
  70.     //    File filters for StandardGetFile should never be asked
  71.     //    to filter directories. We test here for that problem.
  72.     //
  73.  
  74.     if (cipbp->hFileInfo.ioFlAttrib & ioDirMask)
  75.         DebugStr ("\pA directory has been passed to the Standard File filter.");
  76.     else if (cipbp->hFileInfo.ioFlFndrInfo.fdCreator != 'CWIE')
  77.         dropIt = true;
  78.  
  79.     return dropIt;
  80. }
  81.  
  82. static pascal Boolean ModalFilterYDProc (DialogPtr, EventRecord *event, short *, void *)
  83. {
  84.     if (event->what) SysBeep (-1);
  85.     return false;
  86. }
  87.  
  88. static pascal DialogItemIndex DialogHookYDProc (DialogItemIndex index, DialogPtr dialog, void *)
  89. {
  90.     static const SInt16 kCustomItemCount = 2;
  91.  
  92.     if (index >= 1 && index <= kCustomItemCount)
  93.         (void) ToggleDialogCheckBox (dialog, index + MOASI_GetFirstCustomItemIndex ( ));
  94.     else
  95.     {
  96. /*
  97.         Str15 message;
  98.         NumToString (index,message);
  99.         DebugStr (message);
  100. */
  101.     }
  102.  
  103.     return index;
  104. }
  105.  
  106. static pascal OSErr MyStandardGetFile (void)
  107. {
  108.     OSErr err = noErr;
  109.  
  110.     FileFilterUPP upp = NewFileFilterProc (StandardFileFilterProc);
  111.  
  112.     if (!upp)
  113.         err = MemError ( );
  114.     else
  115.     {
  116.         StandardFileReply    sfr;
  117.         SFTypeList            sft        = { 'TEXT' };
  118.  
  119.         err = MOASI_StandardGetFile (upp,1,sft,&sfr);
  120.  
  121.         DisposeRoutineDescriptor (upp);
  122.  
  123.         if (!err && sfr.sfGood)
  124.         {
  125.             err = FSMakeFSSpec (sfr.sfFile.vRefNum, sfr.sfFile.parID, sfr.sfFile.name, &(sfr.sfFile));
  126.             // we do this just to verify we have some semblance of valid data
  127.         }
  128.     }
  129.  
  130.     return err;
  131. }
  132.  
  133. static pascal OSErr MyStandardGetFilePreview (void)
  134. {
  135.     OSErr err = noErr;
  136.  
  137.     FileFilterUPP upp = NewFileFilterProc (StandardFileFilterProc);
  138.  
  139.     if (!upp)
  140.         err = MemError ( );
  141.     else
  142.     {
  143.         StandardFileReply    sfr;
  144.         SFTypeList            sft        = { 'TEXT' };
  145.  
  146.         err = MOASI_StandardGetFilePreview (upp,1,sft,&sfr);
  147.  
  148.         DisposeRoutineDescriptor (upp);
  149.  
  150.         if (!err && sfr.sfGood)
  151.         {
  152.             err = FSMakeFSSpec (sfr.sfFile.vRefNum, sfr.sfFile.parID, sfr.sfFile.name, &(sfr.sfFile));
  153.             // we do this just to verify we have some semblance of valid data
  154.         }
  155.     }
  156.  
  157.     return err;
  158. }
  159.  
  160. static pascal OSErr MyStandardOpenDialog (void)
  161. {
  162.     StandardFileReply reply;
  163.  
  164.     return MOASI_StandardOpenDialog (&reply);
  165. }
  166.  
  167. static pascal OSErr MyStandardPutFile (void)
  168. {
  169.     StandardFileReply reply;
  170.  
  171.     return MOASI_StandardPutFile ("\pPrompt","\pDefaultFileName",&reply);
  172. }
  173.  
  174. static pascal OSErr MyCustomGetFile (void)
  175. {
  176.     OSErr err = noErr;
  177.  
  178.     FileFilterYDUPP fileFilterUPP = NewFileFilterYDProc (CustomFileFilterProc);
  179.  
  180.     if (!fileFilterUPP)
  181.         err = MemError ( );
  182.     else
  183.     {
  184.         ModalFilterYDUPP eventFilterUPP = NewModalFilterYDProc (ModalFilterYDProc);
  185.  
  186.         if (!eventFilterUPP)
  187.             err = MemError ( );
  188.         else
  189.         {
  190.             DlgHookYDUPP yduppDialogHook = NewDlgHookYDProc (DialogHookYDProc);
  191.  
  192.             if (!yduppDialogHook)
  193.                 err = MemError ( );
  194.             else
  195.             {
  196.                 StandardFileReply    sfr;
  197.                 SFTypeList            sft                = { 'TEXT' };
  198.                 void                *context        = nil;
  199.             
  200.                 err = MOASI_CustomGetFile (fileFilterUPP,1,sft,&sfr,kResID_DialogItemList_CustomGetFile,yduppDialogHook,eventFilterUPP,context);
  201.  
  202.                 DisposeRoutineDescriptor (yduppDialogHook);
  203.             }
  204.             DisposeRoutineDescriptor (eventFilterUPP);
  205.         }
  206.         DisposeRoutineDescriptor (fileFilterUPP);
  207.     }
  208.  
  209.     return err;
  210. }
  211.  
  212. static pascal OSErr MyCustomGetFilePreview (void)
  213. {
  214.     OSErr err = noErr;
  215.  
  216.     FileFilterYDUPP fileFilterUPP = NewFileFilterYDProc (CustomFileFilterProc);
  217.  
  218.     if (!fileFilterUPP)
  219.         err = MemError ( );
  220.     else
  221.     {
  222.         ModalFilterYDUPP eventFilterUPP = NewModalFilterYDProc (ModalFilterYDProc);
  223.  
  224.         if (!eventFilterUPP)
  225.             err = MemError ( );
  226.         else
  227.         {
  228.             DlgHookYDUPP yduppDialogHook = NewDlgHookYDProc (DialogHookYDProc);
  229.  
  230.             if (!yduppDialogHook)
  231.                 err = MemError ( );
  232.             else
  233.             {
  234.                 StandardFileReply    sfr;
  235.                 SFTypeList            sft                = { 'TEXT' };
  236.                 void                *context        = nil;
  237.             
  238.                 err = MOASI_CustomGetFilePreview (fileFilterUPP,1,sft,&sfr,kResID_DialogItemList_CustomGetFile,yduppDialogHook,eventFilterUPP,context);
  239.  
  240.                 DisposeRoutineDescriptor (yduppDialogHook);
  241.             }
  242.             DisposeRoutineDescriptor (eventFilterUPP);
  243.         }
  244.         DisposeRoutineDescriptor (fileFilterUPP);
  245.     }
  246.  
  247.     return err;
  248. }
  249.  
  250. static pascal OSErr MyCustomPutFile (void)
  251. {
  252.     OSErr err = noErr;
  253.  
  254.     ModalFilterYDUPP eventFilterUPP = NewModalFilterYDProc (ModalFilterYDProc);
  255.  
  256.     if (!eventFilterUPP)
  257.         err = MemError ( );
  258.     else
  259.     {
  260.         DlgHookYDUPP yduppDialogHook = NewDlgHookYDProc (DialogHookYDProc);
  261.  
  262.         if (!yduppDialogHook)
  263.             err = MemError ( );
  264.         else
  265.         {
  266.             StandardFileReply    sfr;
  267.             SFTypeList            sft                = { 'TEXT' };
  268.         
  269.             err = MOASI_CustomPutFile ("\pPrompt","\pDefaultFileName",&sfr,kResID_DialogItemList_CustomPutFile,yduppDialogHook,eventFilterUPP,nil);
  270.  
  271.             DisposeRoutineDescriptor (yduppDialogHook);
  272.         }
  273.         DisposeRoutineDescriptor (eventFilterUPP);
  274.     }
  275.  
  276.     return err;
  277. }
  278.  
  279. static pascal Boolean ModalFilterProc (DialogRef dialog, EventRecord *event, short *itemHit)
  280. {
  281.     return StdFilterProc (dialog,event,itemHit);
  282. }
  283.  
  284. void main (void)
  285. {
  286.     if (InitMac ( ))
  287.         SysBeep (-1);
  288.     else
  289.     {
  290.         DialogRef dlgRef = GetNewDialog (129,nil,(WindowRef)-1);
  291.         if (dlgRef)
  292.         {
  293.             ModalFilterUPP modalFilterUPP = NewModalFilterProc (ModalFilterProc);
  294.             if (modalFilterUPP)
  295.             {
  296.                 enum
  297.                 {
  298.                     kDialogItemIndex_PushButton_Quit = kStdOkItemIndex,
  299.                     kDialogItemIndex_PushButton_StandardGetFile,
  300.                     kDialogItemIndex_PushButton_StandardPutFile,
  301.                     kDialogItemIndex_PushButton_StandardOpenFile,
  302.                     kDialogItemIndex_PushButton_CustomGetFile,
  303.                     kDialogItemIndex_PushButton_CustomPutFile,
  304.                     kDialogItemIndex_PushButton_StandardGetFilePreview,
  305.                     kDialogItemIndex_PushButton_CustomGetFilePreview,
  306.                     kDialogItemIndex_CheckBox_ForceStandardFile
  307.                 };
  308.  
  309.                 short itemHit;
  310.  
  311.                 SetDialogDefaultItem (dlgRef,kDialogItemIndex_PushButton_Quit);
  312.                 SetDialogTracksCursor (dlgRef,true);
  313.  
  314. #if TARGET_CARBON
  315.  
  316.                 //
  317.                 //    Standard File doesn't exist in Carbon,
  318.                 //    so remove the check-box which would force
  319.                 //    us to use it (kDialogItemIndex_CheckBox_ForceStandardFile).
  320.                 //
  321.  
  322.                 ShortenDITL (dlgRef,1);
  323. #endif
  324.  
  325.                 do
  326.                 {
  327.                     OSErr err = noErr;
  328.  
  329.                     MoveableModalDialog (modalFilterUPP,&itemHit);
  330.  
  331.                     switch (itemHit)
  332.                     {
  333.  
  334. #if !TARGET_CARBON
  335.                         case kDialogItemIndex_CheckBox_ForceStandardFile :
  336.  
  337.                             (void) MOASI_EnableDisableNav (!ToggleDialogCheckBox (dlgRef,kDialogItemIndex_CheckBox_ForceStandardFile));
  338.                             break;
  339. #endif
  340.  
  341.                         case kDialogItemIndex_PushButton_StandardGetFilePreview :
  342.  
  343.                             err = MyStandardGetFilePreview ( );
  344.                             break;
  345.  
  346.                         case kDialogItemIndex_PushButton_CustomGetFilePreview :
  347.  
  348.                             err = MyCustomGetFilePreview ( );
  349.                             break;
  350.  
  351.                         case kDialogItemIndex_PushButton_StandardGetFile :
  352.  
  353.                             err = MyStandardGetFile ( );
  354.                             break;
  355.  
  356.                         case kDialogItemIndex_PushButton_StandardPutFile :
  357.  
  358.                             err = MyStandardPutFile ( );
  359.                             break;
  360.  
  361.                         case kDialogItemIndex_PushButton_StandardOpenFile :
  362.  
  363.                             err = MyStandardOpenDialog ( );
  364.                             break;
  365.  
  366.                         case kDialogItemIndex_PushButton_CustomGetFile :
  367.  
  368.                             err = MyCustomGetFile ( );
  369.                             break;
  370.  
  371.                         case kDialogItemIndex_PushButton_CustomPutFile :
  372.  
  373.                             err = MyCustomPutFile ( );
  374.                             break;
  375.                     }
  376.  
  377.                     if (err) SysBeep (-1);
  378.                 }
  379.                 while (itemHit != kDialogItemIndex_PushButton_Quit);
  380.  
  381.                 DisposeRoutineDescriptor (modalFilterUPP);
  382.             }
  383.             DisposeDialog (dlgRef);
  384.         }
  385.     }
  386. }
  387.